﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using DAL;
using System.Data;

public partial class Report : System.Web.UI.Page
{
    DataClassesDataContext dc = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cmbSscq.DataSource = dc.css_cqxx;
            cmbSscq.ValueField = "cqbh";
            cmbSscq.TextField = "cqmc";
            cmbSscq.DataBind();
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gridView.Columns["t_sscq"], "css_cqxx", "cqmc", "cqbh");
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gridView.Columns["t_dwxz"], "css_dwxz", "xzmc", "xzbh");
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gridView.Columns["t_fzjg"], "css_fzjg", "jgmc", "jgbh");
        }
    }
    protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        string SqlFormat = @" SELECT dwbh,
                                    dwmc,
                                    sscq,
                                    fwdz,
                                    zh,
                                    fzr,
                                    fzrq,
                                    fzjg,
                                   dwxz
                                    FROM css_dwxx
                                    WHERE {0} 1=1
                                    ORDER BY dwbh
                                ";
        StringBuilder strb = new StringBuilder();
        List<SqlParameter> Params = new List<SqlParameter>();
        if (!string.IsNullOrEmpty(txtDwmc.Text.Trim()))
        {
            strb.Append("dwmc like ''+@dwmc+'%'  AND ");
            Params.Add(new SqlParameter("@dwmc", txtDwmc.Text.ToString()));
        }
        if (!string.IsNullOrEmpty(cmbSscq.Text.Trim()))
        {
            strb.Append("sscq = @cqbh  AND ");
            Params.Add(new SqlParameter("@cqbh", Convert.ToInt32(cmbSscq.Value.ToString())));
        }
        if (!string.IsNullOrEmpty(txtFwdz.Text.Trim()))
        {
            strb.Append("fwdz like ''+@fwdz+'%'  AND ");
            Params.Add(new SqlParameter("@fwdz", txtFwdz.Text.ToString()));
        }
        if (!string.IsNullOrEmpty(txtZh.Text.Trim()))
        {
            strb.Append("zh like ''+@zh+'%' AND ");
            Params.Add(new SqlParameter("@zh", txtZh.Text.Trim()));
        }
        if (!string.IsNullOrEmpty(txtFzrq.Text.Trim()))
        {
            strb.Append("fzrq like ''+@fzrq+'%' AND ");
            Params.Add(new SqlParameter("@fzrq", txtFzrq.Text.Trim()));
        }

        Session["sqlstr"] = string.Format(SqlFormat, strb.ToString());
        Session["sqlParams"] = Params.ToArray();
        gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
        gridView.DataBind();
    }

    //导出到Excel
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string filename = "社区服务业单位证书名录.xls";
        ExportExcel(filename, "application/ms-excel");
        //gridViewE.WriteXlsToResponse();
    }


    /// <summary>
    /// 导出Excel方法
    /// </summary>
    /// <param name="filename">文件名称</param>
    /// <param name="filentype">文件类型</param>
    private void ExportExcel(string filename, string filentype)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.ContentEncoding = Encoding.UTF8;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8).ToString());
        Response.ContentType = filentype;
        gridView.Page.EnableViewState = true;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        gridView.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

    /// <summary>   
    /// 必须重写VerifyRenderingInServerForm方法，否则报错   
    /// </summary>   
    /// <param name="control"></param>   
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void btnQc_Click(object sender, EventArgs e)
    {
        txtDwmc.Text = "";
        txtFwdz.Text = "";
        txtFzrq.Text = "";
        txtZh.Text = "";
        cmbSscq.Text = "";
        string strSql = @"SELECT dwbh,
                                    dwmc,
                                    sscq,
                                    fwdz,
                                    zh,
                                    fzr,
                                    fzrq,
                                    fzjg,
                                   dwxz
                                    FROM css_dwxx";
        DataTable dt = new DataTable();
        dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
        gridView.DataSource = dt;
        gridView.DataBind();
    }
    protected void gridView_DataBound(object sender, EventArgs e)
    {

    }
    protected void gridView_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {

    }
}